<?php
/**
 * keywords.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-01 22:22
 * @modified 2018-07-01 22:22
 */

class ControllerCatalogKeywords extends Controller
{
    private $error = array();
    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->load->language('catalog/keywords');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('catalog/keywords');
        $this->load->model('localisation/language');
    }

    public function index()
    {
        $this->getList();
    }

    public function delete()
    {
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $id) {
                $this->model_catalog_keywords->deleteKeywords($id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('catalog/keywords', 'user_token=' . $this->session->data['user_token'] . $url, true));
        }

        $this->getList();
    }

    public function add()
    {
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_catalog_keywords->addKeywords($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('catalog/keywords', 'user_token=' . $this->session->data['user_token'] . $url, true));
        }
        $this->getForm();
    }

    public function edit()
    {
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_catalog_keywords->editKeyword($this->request->get['id'], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('catalog/keywords', 'user_token=' . $this->session->data['user_token'] . $url, true));
        }
        $this->getForm();
    }

    protected function getForm()
    {
        $data = $this->baseData();
        $data['text_form'] = !isset($this->request->get['id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
        $url = '';
        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        $breads = new Breads();
        $breads->add(trans('text_home'), uriLink('common/home', 'user_token=' . $this->session->data['user_token']));
        $breads->add(trans('heading_title'), uriLink('catalog/keywords', 'user_token=' . $this->session->data['user_token']));
        $data['breadcrumbs'] = $breads->get();

        $data['languages'] = $this->model_localisation_language->getLanguages();

        if (isset($this->request->post['keywords_description'])) {
            $data['keywords_description'] = $this->request->post['keywords_description'];
        } elseif (isset($this->request->get['id'])) {
            $data['keywords_description'] = $this->model_catalog_keywords->getKeywordsDescriptions($this->request->get['id']);
        } else {
            $data['keywords_description'] = array();
        }

        if (isset($this->request->get['id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $keywords_info = $this->model_catalog_keywords->getKeyword($this->request->get['id']);
        }

        if (isset($this->request->post['device'])) {
            $data['device'] = $this->request->post['device'];
        } elseif (!empty($keywords_info)) {
            $data['device'] = $keywords_info['device'];
        } else {
            $data['device'] = '';
        }

        if (isset($this->request->post['sort_order'])) {
            $data['sort_order'] = $this->request->post['sort_order'];
        } elseif (!empty($keywords_info)) {
            $data['sort_order'] = $keywords_info['sort_order'];
        } else {
            $data['sort_order'] = '';
        }

        if (isset($this->request->post['status'])) {
            $data['status'] = $this->request->post['status'];
        } elseif (!empty($keywords_info)) {
            $data['status'] = $keywords_info['status'];
        } else {
            $data['status'] = '';
        }

        if (!isset($this->request->get['id'])) {
            $data['action'] = $this->url->link('catalog/keywords/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
        } else {
            $data['action'] = $this->url->link('catalog/keywords/edit', 'user_token=' . $this->session->data['user_token'] . '&id=' . $this->request->get['id'] . $url, true);
        }

        $data['cancel'] = $this->url->link('catalog/keywords', 'user_token=' . $this->session->data['user_token'] . $url, true);
        $this->response->setOutput($this->load->view('catalog/keywords_form', $data));
    }

    protected function getList()
    {
        $data = $this->baseData();

        $url = '';
        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
            $url .= '&page=' . $this->request->get['page'];
        } else {
            $page = 1;
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array)$this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }

        $breads = new Breads();
        $breads->add(trans('text_home'), uriLink('common/dashboard', 'user_token=' . $this->session->data['user_token']));
        $breads->add(trans('heading_title'), uriLink('catalog/keywords', 'user_token=' . $this->session->data['user_token']));
        $data['breadcrumbs'] = $breads->get();

        $data['keywords'] = array();
        $filter_data = array(
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' => $this->config->get('config_limit_admin')
        );
        $keywords_total = $this->model_catalog_keywords->getTotalKeywords();
        $results = $this->model_catalog_keywords->getKeywords($filter_data);
        foreach ($results as $result) {
            $data['keywords'][] = array(
                'id' => $result['id'],
                'device' => $result['device'],
                'sort_order' => $result['sort_order'],
                'status' => $result['status'],
                'keyword' => $result['keyword'],
                'edit' => uriLink('catalog/keywords/edit', 'id=' . $result['id'] . "&user_token=" . $this->session->data['user_token'])
            );
        }
        $pagination = new Pagination();
        $pagination->total = $keywords_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('catalog/keywords', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($keywords_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($keywords_total - $this->config->get('config_limit_admin'))) ? $keywords_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $keywords_total, ceil($keywords_total / $this->config->get('config_limit_admin')));

        $data['add'] = $this->url->link('catalog/keywords/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
        $data['delete'] = $this->url->link('catalog/keywords/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
        $this->response->setOutput($this->load->view('catalog/keywords_list', $data));
    }

    protected function baseData()
    {
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        return $data;
    }

    protected function validateForm()
    {
        if (!$this->user->hasPermission('modify', 'catalog/option')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        return !$this->error;
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', 'catalog/keywords')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        return !$this->error;
    }
}